<template>
	<view class="folderPropertieSub"  :class="APPTheme">
		<uni-nav-bar fixed status-bar left-icon="back" :title="title" :color="APPTheme=='theme-light'?'#000000':'#ffffff'" :background-color="APPTheme=='theme-light'?'#ffffff':'#000000'" @clickLeft="$lizhao.lznavigate.lzNavigateBack()">
			 <view slot="right">
				 <text @click="addOrgFn">添加</text>
			 </view>
		</uni-nav-bar>
		
		<view class="folder-list">
			<view class="list-item" v-for="(item,index) in org.employeesList" :key="item.id">
				<view class="i-left lz-ellipsis-1">
					<text>{{item.remarksName||item.name}}</text>
					<text v-if="item.boolCreator!=0" class="left-right" style="color: #aa55ff;margin-right: 5px;">用户</text>
					<text v-if="item.boolCreator==0" class="left-right">创建者</text>
				</view>
				<view class="i-right" v-if="item.boolCreator!=0" @click="closeTagFn(item,'employeesList')"><uni-icons type="clear" size="24" color="#888"></uni-icons></view>
			</view>
			<view class="list-item" v-for="(item,index) in org.departmentsList" :key="item.id">
				<view class="i-left">{{item.orgName}}
					<text class="left-right" style="color: #007AFF;margin-right: 5px;">组织</text>
				</view>
				
				<view class="i-right" @click="closeTagFn(item,'departmentsList')"><uni-icons type="clear" size="24" color="#888"></uni-icons></view>
			</view>
			<view class="list-item" v-for="(item,index) in org.roleList" :key="item.id">
				<view class="i-left">{{item.roleName}}
					<text class="left-right" style="color: #ffaa00;margin-right: 5px;">角色</text>
				</view>
				<view class="i-right" @click="closeTagFn(item,'roleList')"><uni-icons type="clear" size="24" color="#888"></uni-icons></view>
			</view>
		</view>
		<u-no-network></u-no-network>
	</view>
</template>

<script>
	let that
	import {mapGetters,mapMutations} from 'vuex'
	export default {
		computed:{
			...mapGetters(['APPTheme', 'userInfo', 'api']),
		},
		data() {
			return {
				title:'谁可以管理',
				org:{//已选择
					employeesList:[],//已选择人
					departmentsList:[],//已选择组织
					roleList:[],//已选择的角色
				},
				transmission:null,//传过来的值
				roleType:null,//需要设置的权限 0谁可以管理，1谁可以编辑上传，2谁可以查看
			}
		},
		onLoad(e) {
			that = this
			that.transmission = JSON.parse(e.option)
			console.log(e);
			that.roleType=e.roleType//需要设置的权限 0谁可以管理，1谁可以编辑上传，2谁可以查看
			if(e.roleType==0){
				that.title="谁可以管理"
			}else if(e.roleType==1){
				that.title="谁可以编辑/上传"
			}else if(e.roleType==2){
				that.title="谁可以查看"
			}
			
			//监听到已选人
			uni.$off('mail_confirm')
			uni.$on('mail_confirm',(res)=>{
				that.processSelectedPeople(res)
			})
			//获取文件夹管理者
			that.getFilingCabinetsRoleList()
		},
		onShow() {
			//设置主题颜色
			that.GLOBAL.setThemeColor(that.APPTheme)
		},
		methods: {
			//获取文件夹管理者 roleStatus用户类别 0为用户 1组织 2为角色
			getFilingCabinetsRoleList(){
				let req={
					filingId:that.transmission.id,//文件柜id
					roleType:that.roleType,//0谁可以管理，1谁可以编辑上传，2谁可以查看
				}
				that.GLOBAL.doGet(that.api.GLOBAL_admin_rest_biz_filingCabinetsRole_list,req).then(res=>{
					if(res.code=='0'&&res.data){
						//格式化数据
						that.formatdataFn(res.data)
					}
				})
			},
			//格式化数据
			formatdataFn(data){
				let org={//已选择
					employeesList:[],//已选择人
					departmentsList:[],//已选择组织
					roleList:[],//已选择的角色
				}
				let {keys,values,entries}=Object
				for(let [keys,values] of entries(data)){
					if(values.roleStatus==0){//用户类别 0为用户 1组织 2为角色
						let req={
							name:values.useName,
							id:values.userid,
							...values,
						}
						org.employeesList.push(req)
					}else if(values.roleStatus==1){
						let req={
							orgName:values.useName,
							id:values.userid,
							...values,
						}
						org.departmentsList.push(req)
					}else if(values.roleStatus==2){
						let req={
							roleName:values.useName,
							id:values.userid,
							...values,
						}
						org.roleList.push(req)
					}
				}
				that.org=org
			},
			//处理已选的人
			processSelectedPeople(res){
				if(res.name=='角色'){//角色
					that.org.roleList=that.org.roleList.concat(res.departmentsList)
				}else{//组织
					that.org.departmentsList=that.org.departmentsList.concat(res.departmentsList)
					that.org.employeesList=that.org.employeesList.concat(res.employeesList)
				}
				//过滤人
				that.filterUserFn()
			},
			//过滤人
			filterUserFn(){
				let obj = {};
				that.org.employeesList = that.org.employeesList.reduce((cur,next) => {
				    obj[next.id] ? "" : obj[next.id] = true && cur.push(next);
				    return cur;
				},[]) 
				let obj2 = {};
				that.org.departmentsList = that.org.departmentsList.reduce((cur,next) => {
				    obj2[next.id] ? "" : obj2[next.id] = true && cur.push(next);
				    return cur;
				},[]) 
				let obj3 = {};
				that.org.roleList = that.org.roleList.reduce((cur,next) => {
				    obj3[next.id] ? "" : obj3[next.id] = true && cur.push(next);
				    return cur;
				},[]) 
				//完成提交设置
				that.filingCabinetsRole_saveFn()
			},
			//添加
			addOrgFn(){
				uni.navigateTo({url: '/pages/workbench/selectionMethod/selectionMethod?title='+that.title+'&data='+JSON.stringify(that.org)});
			},
			//删除tag,name为删除哪个数组
			closeTagFn(item,type){
				console.log(item,type);
				if(item.boolCreator==0){
					uni.showToast({
						title: '创建者不可删除',
						icon:"none"
					});
					return
				}
				uni.showModal({
					title: '删除',
					content: `确定删除当前选项?`,
					success: res => {
						if(res.confirm){
							that.org[type]=that.org[type].filter((res)=>{
								if(res.id!=item.id){
									return res
								}
							})
							//完成提交设置
							that.filingCabinetsRole_delete(item.id)
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
			//删除管理者
			filingCabinetsRole_delete(ids){
				let req={
					filingId:that.transmission.id,//文件柜id
					ids:ids
				}
				that.GLOBAL.doGet(that.api.GLOBAL_admin_rest_biz_filingCabinetsRole_delete,req).then(res=>{
					if(res.code=='0'&&res.data){
						uni.showToast({
							title: res.data.msg
						});
					}
				})
			},
			
			//完成提交设置
			filingCabinetsRole_saveFn(){
				let userIds=that.org.employeesList.map((res)=>{return res.id})
				let orgIds=that.org.departmentsList.map((res)=>{return res.id})
				let roleIds=that.org.roleList.map((res)=>{return res.id})
				let req={
					filingId:that.transmission.id,//文件柜id
					roleType:that.roleType,//0谁可以管理，1谁可以编辑上传，2谁可以查看
					userIds:userIds.join(','),
					orgIds:orgIds.join(','),
					roleIds:roleIds.join(',')
				}
				that.GLOBAL.doGet(that.api.GLOBAL_admin_rest_biz_filingCabinetsRole_save,req).then(res=>{
					if(res.code=='0'&&res.data){
						uni.showToast({
							title: res.data.msg
						});
					}
				})
			}
			
		},
		onBackPress() {
		}
	}
</script>

<style lang="scss" scoped>
	.folder-list{
		box-sizing: border-box;height: 100%;
		.list-item{
			display: flex;width: 100%;align-items: center;padding: 10rpx 20rpx;box-sizing: border-box;
			.i-left{
				font-size: $font_size-16;display: flex;justify-content: space-between;align-items: center;flex: 1;
				.left-right{
					color: #888;font-size: $font_size-12;
				}
			}
			.i-right{
				font-size: $font_size-16;
			}
		}
		.list-item:active{
			background-color: $bg-active-light;
		}
		
		// 暂无数据
		.u-empty{
			position: fixed;top:0;height: 100%;width: 100%;z-index: 0;
		}
	}
</style>
